package com.csu.cphone.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.RecyclePhonePojo;
import com.csu.cphone.pojo.RecycleProblemPojo;
import com.csu.cphone.service.RecyclePhoneService;
import com.csu.cphone.service.RecycleService;
import com.csu.cphone.vo.RecyclePhoneProblemsVo;
import com.csu.cphone.vo.RecyclePhoneTreeVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：RecycleController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月4日 上午10:37:18
 * @company:步步高教育电子有限公司
 */
@Controller
@RequestMapping("/recycle")
@Api(value = "recycle", description = "手机回收")
public class RecycleController
{
    @Autowired
    private RecycleService recycleService;

    @Autowired
    private RecyclePhoneService recyclePhoneService;

    @ResponseBody
    @ApiOperation(value = "获取机器回收问题", notes = "根据机型获取回收需要回到的问题")
    @RequestMapping(value = "/getRecycleProblems/{phoneId}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleProblemPojo>> getRecycleProblems(HttpServletRequest request,
            @ApiParam(value = "手机id") @PathVariable(value = "phoneId") int phoneId)
    {
        ResultVo<List<RecycleProblemPojo>> result = new ResultVo<List<RecycleProblemPojo>>();
        List<RecycleProblemPojo> problems = recycleService.getRecycleProblems(phoneId);
        result.setData(problems);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "报价结果", notes = "根据回收问题计算报价")
    @RequestMapping(value = "/offer", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Double> offer(HttpServletRequest request, @RequestBody RecyclePhoneProblemsVo phoneProblems)
    {
        ResultVo<Double> result = new ResultVo<Double>();

        Double offer = recycleService.offer(phoneProblems);
        result.setData(offer);
        return result;
    }

    /*
     * @ResponseBody
     * 
     * @ApiOperation(value = "添加回收问题", notes = "添加手机回收需要回答的问题")
     * 
     * @RequestMapping(value = "/addRecycleProblem", method = {
     * RequestMethod.POST, RequestMethod.GET }, produces = {
     * "application/json; charset=utf-8" }) public ResultVo<RecycleProblemPojo>
     * addRecycleProblem(HttpServletRequest request,
     * 
     * @RequestBody RecycleProblemPojo phoneProblem) {
     * ResultVo<RecycleProblemPojo> result = new ResultVo<RecycleProblemPojo>();
     * RecycleProblemPojo problem =
     * recycleService.addRecycleProblem(phoneProblem); result.setData(problem);
     * return result; }
     */

    @ResponseBody
    @ApiOperation(value = "热们回收机型", notes = "首页展示热门机型列表")
    @RequestMapping(value = "/hotRecycles", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecyclePhonePojo>> hotRecycles(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<List<RecyclePhonePojo>> result = new ResultVo<List<RecyclePhonePojo>>();

        List<RecyclePhonePojo> hotList = recycleService.getHotRecycleList();

        result.setData(hotList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "获取可回收机型", notes = "获取可回收机型列表")
    @RequestMapping(value = "/getRecyclePhones/{brandId}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecyclePhonePojo>> getRecyclePhones(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "品牌id") @PathVariable(value = "brandId") int brandId)
    {
        ResultVo<List<RecyclePhonePojo>> result = new ResultVo<List<RecyclePhonePojo>>();

        List<RecyclePhonePojo> recycleList = recycleService.getRecyclePhoneList(brandId);

        result.setData(recycleList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "获取回收修机型", notes = "获取可回收机型列表树")
    @RequestMapping(value = "/getRecyclePhones", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecyclePhoneTreeVo>> getMaintainPhones(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<List<RecyclePhoneTreeVo>> result = new ResultVo<List<RecyclePhoneTreeVo>>();

        List<RecyclePhoneTreeVo> treeList = recycleService.getMaintainPhoneTreeList();

        result.setData(treeList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "添加回收手机", notes = "添加回收的手机")
    @RequestMapping(value = "/addRecyclePhone", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecyclePhonePojo> addRecyclePhone(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "品牌id") @RequestParam(value = "brandId", required = true) int brandId,
            @ApiParam(value = "手机名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url,
            @ApiParam(value = "是否用于回收:0不用,1用") @RequestParam(value = "isUseRecycle", required = true) int isUseRecycle,
            @ApiParam(value = "是否用于热门回收:0不用,1用") @RequestParam(value = "isUseHotRecycle", required = true) int isUseHotRecycle,
            @ApiParam(value = "最高回收价格") @RequestParam(value = "totalPrice", required = false) double totalPrice)
    {
        ResultVo<RecyclePhonePojo> result = new ResultVo<RecyclePhonePojo>();
        RecyclePhonePojo phone = recycleService.addPhone(brandId, name, url, isUseRecycle, totalPrice, isUseHotRecycle);
        result.setData(phone);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "添加回收热门机型", notes = "添加热门回收机型")
    @RequestMapping(value = "/addHotRecycle/{phoneId}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> addHotRecycle(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "手机id") @RequestParam(value = "phoneId", required = true) int phoneId)
    {
        ResultVo<Integer> result = new ResultVo<Integer>();
        Integer data = recycleService.addToHotRecycleList(phoneId);
        result.setData(data);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询维回收手机信息", notes = "获取回收手机信息")
    @RequestMapping(value = "/recyclePhoneInfo/{recyclePhoneId}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecyclePhonePojo> recyclePhoneInfo(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "回收机手机id") @PathVariable(value = "recyclePhoneId", required = true) Integer recyclePhoneId)
    {
        ResultVo<RecyclePhonePojo> result = new ResultVo<RecyclePhonePojo>();
        RecyclePhonePojo phone = recyclePhoneService.queryById(recyclePhoneId);
        result.setData(phone);
        return result;
    }

}
